// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env:"githome-e5vzs"
})
const db = cloud.database()
const _ = db.command
// 云函数入口函数

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let res = await cloud.database().collection('jijiuzhushou').orderBy('id', 'desc').limit(1).get()
  let idmax = parseInt(res.data[0].id)
  idmax = idmax + 1
  idmax = idmax.toString()
  let img = event.userInfo.avatarUrl
  let item = event.a.fangan
  let smtitle = event.a.smtitle
  let title = event.a.title
  let nickName = event.userInfo.nickName
  var myDate = new Date();
  var time =  myDate.toLocaleString( )
  console.log(img)
  console.log(item)
  console.log(title)

  return await db.collection('jijiuzhushou').add({
    data: {
      id: idmax,
      img: img,
      item: item,
      title: title,
      time: time,
      smtitle:smtitle,
      name:nickName,
      openid:event.openid
    }
  })
}